package com.example.mqttclient.utils;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.util.Log;

import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import com.example.mqttclient.DevicesDemoActivity;
import com.example.mqttclient.R;

public class NotificationUtils {

    private static final String CHANNEL_ID = "weather_channel";
    private static final int NOTIFICATION_ID = 1;
    // 天气状态与图标的映射（可扩展更多状态）
    private static final String[] WEATHER_ICONS = {
            "晴", "多云", "阴", "雨", "雪"
    };

    public static void showWeatherNotification(Context context, String temperature, String weatherText) {

        // 1. 创建通知渠道（Android 8.0+）
        createNotificationChannel(context);

        // 2. 构建通知意图
        Intent intent = new Intent(context, DevicesDemoActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(
                context,
                0,
                intent,
                PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
        );

        // 3. 加载天气图标（智能匹配天气状态）
        int iconResId = getWeatherIconResId(weatherText);
        Bitmap largeIcon = BitmapFactory.decodeResource(context.getResources(), iconResId);

        // 4. 构建通知（使用大文本样式）
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
                .setSmallIcon(R.drawable.ic_light)          // 小图标（必须纯色）
                .setLargeIcon(largeIcon)                    // 大图标
                .setContentTitle("实时天气更新")             // 标题更清晰
                .setContentText(weatherText + "，温度：" + temperature + "°C")
                .setStyle(new NotificationCompat.BigTextStyle()  // 大文本样式，适配长内容
                        .bigText("当前北京天气：" + weatherText + "\n实时温度：" + temperature + "°C"))
                .setPriority(NotificationCompat.PRIORITY_HIGH)  // 提高优先级
                .setContentIntent(pendingIntent)
                .setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_ALL);         // 开启声音、震动等默认效果

        // 5. 显示通知（兼容低版本）
        // 直接获取 NotificationManagerCompat
        NotificationManagerCompat manager = NotificationManagerCompat.from(context);
// 显示通知
        manager.notify(NOTIFICATION_ID, builder.build());
        Log.d("NotificationUtils", "天气通知已发送：" + weatherText + " " + temperature + "°C");
    }

    /**
     * 根据天气文字匹配图标（可扩展更多天气类型）
     */
    private static int getWeatherIconResId(String weatherText) {
        // 简单匹配示例，可替换为更精准的天气图标逻辑
        if (weatherText.contains("晴")) return R.drawable.ic_light;
        if (weatherText.contains("多云")) return R.drawable.ic_light;
        if (weatherText.contains("阴")) return R.drawable.ic_light;
        if (weatherText.contains("雨")) return R.drawable.ic_light;
        if (weatherText.contains("雪")) return R.drawable.ic_light;
        return R.drawable.ic_light; // 默认图标
    }

    /**
     * 独立的通知渠道创建方法（更清晰）
     */
    private static void createNotificationChannel(Context context) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return;

        NotificationChannel channel = new NotificationChannel(
                CHANNEL_ID,
                "天气通知",
                NotificationManager.IMPORTANCE_DEFAULT
        );
        channel.setDescription("显示实时天气状态和温度");
        channel.enableLights(true);
        channel.setLightColor(0xFF6699FF); // 通知灯颜色
        channel.enableVibration(true);
        channel.setVibrationPattern(new long[]{100, 200, 100, 200}); // 震动节奏

        NotificationManager manager = context.getSystemService(NotificationManager.class);
        manager.createNotificationChannel(channel);
        Log.d("NotificationUtils", "通知渠道已创建（Android 8.0+）");
    }
}